package fr.licpro.filebox.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fr.licpro.filebox.R;
import fr.licpro.filebox.model.File;

/**
 * Adapter for display list of file
 * @author Abdessamad EL FATTAKE
 * @author Sylvain LE GOUELLEC
 * @version 1.0.0
 * @since 1.0.0
 */
public class ListItemAdapter extends ArrayAdapter<File>
{
	/**
	 * Context of application
	 */
	@SuppressWarnings("unused")
	private Context mContext;
	
	/**
	 * View resource
	 */
	private int mResource;

	/**
	 * Constructor
	 * @param context Application context
	 * @param resource View resource
	 * @param objects List of objects to show in ListView
	 */
	public ListItemAdapter(Context context, int resource, List<File> objects) {
		super(context, resource, objects);

		this.mContext = context;
		this.mResource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		Holder holder;
		
		if(convertView == null)
		{
			convertView = LayoutInflater.from(getContext()).inflate(mResource, parent, false);
			
			holder = new Holder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
			holder.fileName = (TextView) convertView.findViewById(R.id.filename_textview);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (Holder) convertView.getTag();
		}
		
		File file = getItem(position);
		
		if(file.getFileType() != null)
		{
			switch (file.getFileType()) 
			{
				case HTML: holder.icon.setImageResource(R.drawable.html);
					break;
					
				case PDF: holder.icon.setImageResource(R.drawable.pdf);
					break;
					
				case JPEG: holder.icon.setImageResource(R.drawable.jpg);
					break;
					
				case PNG: holder.icon.setImageResource(R.drawable.png);
					break;
					
				case MP3: holder.icon.setImageResource(R.drawable.mp3);
					break;
					
				case TEXT: holder.icon.setImageResource(R.drawable.default_icon);
					break;
					
				case VCARD: holder.icon.setImageResource(R.drawable.vcard);
					break;
					
				default: holder.icon.setImageResource(R.drawable.default_icon);
					break;
			}
		}
		else
			holder.icon.setImageResource(R.drawable.folder);
		
		holder.fileName.setText(file.getName());
		
		return convertView;
	}
	
	/**
	 * Object to stock view resources
	 */
	class Holder
	{
		ImageView icon;
		TextView fileName;
	}
}
